package com.yishan.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yishan.dao.ModuleDao;
import com.yishan.dao.UserDao;
import com.yishan.dao.UserDao;
import com.yishan.domain.Module;
import com.yishan.domain.User;
import com.yishan.domain.User;
import com.yishan.service.UserService;
import com.yishan.utils.MD5Util;
import java.util.List;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author yishan
 * @Date 2021/4/5 0005 12:56
 * @Version 1.0
 */
@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserDao userDao;

	@Autowired
	private ModuleDao moduleDao;

	@Override
	public void save(User user) {

		// id使用UUID的生成策略来获取
		String id = UUID.randomUUID().toString().replace("-","").substring(0,15);
		user.setId(id);

		//联系方式进行 - - 拼接
		StringBuffer tel = new StringBuffer(user.getTel());
		tel.insert(3,"-");
		tel.insert(8,"-");
		user.setTel(tel.toString());

		userDao.save(user);
	}

	@Override
	public void delete(String id) {
		userDao.delete(id);
	}


	@Override
	public void deleteMany(String ids) {
		String[] idss = ids.split(",");
		for (String s : idss) {
			delete(s);
		}
	}

	@Override
	public void update(User user) {

		//联系方式进行 - - 拼接
		StringBuffer tel = new StringBuffer(user.getTel());
		tel.insert(3,"-");
		tel.insert(8,"-");
		user.setTel(tel.toString());

		userDao.update(user);

	}

	@Override
	public User findById(String id) {

		return userDao.findById(id);

	}

	@Override
	public List<User> findAll() {

		return userDao.findAll();

	}

	@Override
	public PageInfo findAll(int page, int size) {

		PageHelper.startPage(page,size);
		List<User> list = userDao.findAll();
		return new PageInfo(list);

	}

	@Override
	public void updateRole(String userId, String[] roleIds) {
		userDao.deleteRole(userId);

		for (String roleId : roleIds) {
			userDao.updateRole(userId,roleId);
		}
	}

	@Override
	public User login(String email, String zd_pwd) {

		//zd_pwd = MD5Util.md5(zd_pwd);

		return userDao.findByEmailAndPwd(email,zd_pwd);

	}

	@Override
	public List<Module> findModuleById(String id) {

		return moduleDao.findModuleByUserId(id);

	}


}
